import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JOptionPane;

public class MagazineClient {
	private Magazine magazine;
	private MagazineClientFrame frame;
	private boolean isSSLConnection;

	public static void main(String args[]) {
		System.setProperty("javax.net.ssl.keyStore", "clientkeystore.jks");
		System.setProperty("javax.net.ssl.keyStorePassword", "frafele");
		System.setProperty("javax.net.ssl.trustStore", "clienttruststore.jks");
		System.setProperty("javax.net.ssl.trustStorePassword", "frafele");
		new MagazineClient();

	}

	public MagazineClient() {

		isSSLConnection = JOptionPane.showConfirmDialog(frame,
				"Utilizzare una connessione sicura?", "Select connection",
				JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION;
		System.out.println("Client connection secure: " + isSSLConnection);
		try {
			if (!isSSLConnection)
				magazine = (Magazine) Naming.lookup("rmi://localhost/magazine");
			else
				magazine = (Magazine) Naming
						.lookup("rmi://localhost/magazineSSL");

			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						frame = new MagazineClientFrame();
						frame.setVisible(true);
						frame.addSendListener(new SendListener());
						frame.addGetListener(new GetListener());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});

		} catch (MalformedURLException e) {
			System.out.println("Url errata");
		} catch (RemoteException e) {
			System.out.println("Server non raggiungibile");
		} catch (NotBoundException e) {
			System.out.println("Binding non trovato");
		}
	}

	private class GetListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						frame.showNews(magazine.getNews());
					} catch (RemoteException e1) {
						try {
							// Riprovo la connessione
							if (!isSSLConnection)
								magazine = (Magazine) Naming
										.lookup("rmi://localhost/magazine");
							else
								magazine = (Magazine) Naming
										.lookup("rmi://localhost/magazineSSL");

							frame.showNews(magazine.getNews());
						} catch (MalformedURLException | RemoteException
								| NotBoundException e2) {
							JOptionPane.showMessageDialog(frame,
									"Server non raggiungibile", "Errore",
									JOptionPane.ERROR_MESSAGE);
						}
					}

				}
			}).start();
		}
	}

	private class SendListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					String author, title, text;
					author = frame.getAuthor();
					title = frame.getTitle();
					text = frame.getText();
					if (author.equals("") || title.equals("")
							|| text.equals("")) {
						JOptionPane.showMessageDialog(frame,
								"Completare tutti i campi", "Errore",
								JOptionPane.ERROR_MESSAGE);
					} else {
						try {
							magazine.addNews(new News(author, title, text));
							frame.resetWriterFields();
						} catch (RemoteException e1) {
							try {
								// Riprovo la connessione
								if (!isSSLConnection)
									magazine = (Magazine) Naming
											.lookup("rmi://localhost/magazine");
								else
									magazine = (Magazine) Naming
											.lookup("rmi://localhost/magazineSSL");

								magazine.addNews(new News(author, title, text));
								frame.resetWriterFields();
							} catch (MalformedURLException | RemoteException
									| NotBoundException e2) {
								JOptionPane.showMessageDialog(frame,
										"Server non raggiungibile", "Errore",
										JOptionPane.ERROR_MESSAGE);
							}
						}
					}

				}
			}).start();

		}
	}
}
